/*
 * @Author: youngqj youngqj@126.com
 * @Date: 2024-12-04 18:31:16
 * @LastEditors: youngqj youngqj@126.com
 * @LastEditTime: 2024-12-04 19:15:30
 * @FilePath: /mallSystem/core/utils/snowflakex.go
 * @Description:
 *
 * Copyright © Zhejiang Xiaoqu Information Technology Co., Ltd, All Rights Reserved.
 */
package utils

import (
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"log"
	"math/big"
	"strings"

	"github.com/bwmarrin/snowflake"
)

// 初始化 Snowflake 节点
func InitSnowflakeNode(nodeID int64) *snowflake.Node {
	node, err := snowflake.NewNode(nodeID)
	if err != nil {
		log.Fatalf("Failed to initialize Snowflake node: %v", err)
	}
	return node
}

// 使用 Snowflake 生成随机字符串
func GenerateRandomString(node *snowflake.Node) int64 {
	// 生成唯一 ID
	id := node.Generate().Int64()

	// 转为字符串
	return id
}

// 使用 SHA-256 哈希 Snowflake ID 并转换为 Base62
func hashAndEncodeSnowflakeID(id int64) string {
	// 哈希 Snowflake ID
	hash := sha256.Sum256([]byte(fmt.Sprintf("%d", id)))

	// 将哈希结果转为十六进制字符串
	hexStr := hex.EncodeToString(hash[:])

	// 截取部分十六进制字符串并转为 Base62
	num, _ := new(big.Int).SetString(hexStr[:16], 16) // 取前 16 位
	return toBase62(num.Int64())
}

// Base62 字符集
const base62Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

// 将整数转换为 Base62 编码
func toBase62(num int64) string {
	base := int64(len(base62Chars)) // Base62 的基数是 62
	result := ""
	for num > 0 {
		remainder := num % base
		result = string(base62Chars[remainder]) + result
		num = num / base
	}
	return result
}

func GenerateRefreshToken(deviceID string) (string, error) {

	// 假设 Node ID 是 1
	nodeID := GetNodeIDWithPreference("en0")
	fmt.Printf("Generated Node ID: %d\n", nodeID)

	// 初始化 Snowflake 节点
	node := InitSnowflakeNode(nodeID)

	// 生成随机字符串
	randomString := GenerateRandomString(node)
	fmt.Printf("Generated Random String: %v\n", randomString)

	encodedString := hashAndEncodeSnowflakeID(randomString)
	fmt.Printf("Hash + Base62 Encoded String: %s\n", encodedString)

	encryptStr, err := Encrypt(encodedString)
	if err != nil {
		return "", err
	}
	encodeStr := encryptStr + "^_^" + deviceID
	return encodeBase64(encodeStr), nil
}

func RunDemoSnowFlake() {
	// // 假设 Node ID 是 1
	// nodeID := GetNodeIDWithPreference("en0")
	// fmt.Printf("Generated Node ID: %d\n", nodeID)

	// // 初始化 Snowflake 节点
	// node := InitSnowflakeNode(nodeID)

	// // 生成随机字符串
	// randomString := GenerateRandomString(node)
	// fmt.Printf("Generated Random String: %v\n", randomString)

	// encodedString := hashAndEncodeSnowflakeID(randomString)
	// fmt.Printf("Hash + Base62 Encoded String: %s\n", encodedString)
	// fmt.Println()

	refreshToken, _ := GenerateRefreshToken("6a28ccfa-8d74-3ab2-3114-65695666d48e")
	fmt.Printf("refreshToken String: %s\n", refreshToken)
	fmt.Println(1111111)
	base64Strde, _ := decodeBase64(refreshToken) //解码

	fmt.Printf("decode base64  String: %s\n", base64Strde)
	fmt.Println(1111111)
	base64Strded := strings.Split(base64Strde, "^_^")
	decodeString, _ := Decrypt(base64Strded[0])
	fmt.Printf("refreshToken DeString: %s\n", decodeString)

}
